<?php

class Controller_Main extends Controller
{
	public function home()
	{
		$banners = DB::getRows('select id, title from banners order by id desc limit 50');
		
		$this->view->add("banners", $banners);
		return $this->view->render('main/home.tpl');
	}
	
	public function login($aData = array(), $errors = array())
	{
	    if (Request::isPost())
	    {
	        $aData = Request::postVars(array('login', 'password'));
    		$validator = new Validator();
    		$validator->add(new Rule_NotEmpty('login', 'Введите логин'));
    		$validator->add(new Rule_NotEmpty('password', 'Введите пароль'));
    		$validator->add(new Rule_AccountExists('exists'));
    		if ($validator->validate($aData))
    		{
        		$profile = DB::singleRow('select * from accounts where login = ?', $aData['login']);
        		setCurrentUser($profile);
        		setcookie("cuid", $profile['id'], time() + 86400, "/", '.'.HOST_NAME_SHORT);
        		Redirect('/'.$profile['utype']);
    		}
    		else
    		{
    		    $errors = $validator->errors;
    		}
    		
	    }
	    
		$this->view->add(array(
		    'aData'  => $aData,
		    'errors' => $errors,
		));
		return $this->view->render('main/login.tpl');
	}
	
	public function logout()
	{
		unset($_SESSION); 
		setcookie(session_name(), '', time() - (3000 * 86400), '/', '.'.HOST_NAME_SHORT);
		Redirect('/');
	}
}